# 爬取肯德基并写入csv， 创建excel表格
import requests

url = "https://www.kfc.com.cn/kfccda/ashx/GetStoreList.ashx?op=cname"
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36",
    "Cookie": "__utmz=62931061.1732767443.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __utma=62931061.806578909.1732767443.1732767443.1733278636.2; route-cell=ksa; ASP.NET_SessionId=3ogi0v5hpyxoblsbcwnfbp1r; VOLCALBCORS=6d9a312ffe47779f2aa41b5d5a127127|1733408874|1733408856; VOLCALB=6d9a312ffe47779f2aa41b5d5a127127|1733408874|1733408856",

}
params = {
    "cname": "湘潭",
    "pid": "",
    "pageIndex": "1",
    "pageSize": "10",
}

response = requests.post(url, headers=headers, params=params)
response.encoding = response.apparent_encoding

print(response.json())

with open("kendeji.csv", "w", encoding="gbk", newline="") as f:
    file_name = ["storeName", "addressDetail", "pro"]
    writer = csv.DictWriter(f, fieldnames=file_name)
    writer.writeheader()
    for i in response.json()["Table1"]:
        item = {}
        item["storeName"] = i["storeName"]
        item["addressDetail"] = i["addressDetail"]
        item["pro"] = i["pro"]
        writer.writerow(item)
        print(item)